package mt.tests;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Queue;


import mt.Order;
import mt.comm.ServerComm;
import mt.comm.ServerSideMessage;
import mt.comm.ServerSideMessage.Type;
import mt.server.MicroTraderServerDummy;

public class TestServerComm implements ServerComm {
	private MicroTraderServerDummy server;
	private ServerComm serverComm;
	private Queue<ServerSideMessage> messagesToServer;
	private Queue<ServerSideMessage> messagesToClients;
	public static final int PORTO = 8080;
	private ServerSocket serverSocket;
	private Socket socket;

	@Override
	public void start() {
		try{
			serverSocket= new ServerSocket(PORTO);
			System.out.println("Servidor iniciado..." + PORTO);

			while(true){
				socket = serverSocket.accept();
			}

			} catch (IOException e) {

			e.printStackTrace();
			}finally{
			try {
			serverSocket.close();
			} catch (IOException e) {

			System.out.println("Servidor Encerrado");
			}
			}

			
	}

	@Override
	public ServerSideMessage getNextMessage() {
		return messagesToServer.poll();
	}

	@Override
	public boolean hasNextMessage() {
		if (!messagesToServer.isEmpty())
			return true;
		else
			return false;
	}

	@Override
	public void sendOrder(String receiversNickname, Order order) {
		TestServerSideMessage m = new TestServerSideMessage(null, receiversNickname, order);
		messagesToClients.add(m);

	}

	@Override
	public void sendError(String toNickname, String error) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean clientIsConnected(String nickname) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void disconnectClient(String nickname) {
		// TODO Auto-generated method stub

	}

	public void addMessagesToQueue(ServerSideMessage message) {
		messagesToServer.add(message);
	}

}
